package com.schoolcloud.serve.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.serve.entity.LostFoundCategory;
import com.schoolcloud.serve.entity.LostFoundPic;
import com.schoolcloud.serve.entity.LostItems;
import com.schoolcloud.serve.query.LostItemQuery;
import com.schoolcloud.serve.service.LostFoundCategoryService;
import com.schoolcloud.serve.service.LostFoundPicService;
import com.schoolcloud.serve.service.LostItemsService;
import com.schoolcloud.serve.mapper.LostItemsMapper;
import com.schoolcloud.serve.vo.LostFoundInfoVO;
import com.schoolcolud.daomybatisplus.dto.MpPageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongmizfb
 * @description 针对表【lost_items(失物表)】的数据库操作Service实现
 * @createDate 2025-03-15 00:09:02
 */
@Service
@RequiredArgsConstructor
public class LostItemsServiceImpl extends ServiceImpl<LostItemsMapper, LostItems>
        implements LostItemsService {
    private final LostFoundPicService lostFoundPicService;
    private final LostFoundCategoryService lostFoundCategoryService;

    @Override
    public PageDTO<LostItems> getLostItemsPage(LostItemQuery query) {
        String keyword = query.getKeyword();
        Integer lostFoundCategoryId = query.getCategoryId();
        String location = query.getLocation();
        Integer userId = query.getUserId();
        Page<LostItems> page = lambdaQuery()
                .eq(lostFoundCategoryId != null && lostFoundCategoryId > 0, LostItems::getCategoryId, lostFoundCategoryId)
                .eq(StrUtil.isNotBlank(location), LostItems::getLocation, location)
                .eq(userId != null, LostItems::getUserId, userId)
                .like(StrUtil.isNotBlank(keyword), LostItems::getItemName, keyword)
                .or().like(StrUtil.isNotBlank(keyword), LostItems::getDescription, keyword)
                .page(query.toMpPage());
        return  MpPageUtil.of(page, LostItems.class);
    }

    @Override
    @Transactional
    public void saveLostItems(LostFoundInfoVO lostItems) {
        LostItems lostItems1 = new LostItems();
        BeanUtils.copyProperties(lostItems, lostItems1);
        if (lostItems.getLostItemId() == null){
            lostItems.setCreateTime(LocalDateTime.now());
            save(lostItems1);
        }
        if (lostItems.getLostItemId() != null){
            lostItems.setUpdateTime(LocalDateTime.now());
            updateById(lostItems1);
            lostFoundPicService.lambdaUpdate().eq(LostFoundPic::getLostItemId, lostItems.getLostItemId()).remove();
        }
        if (StrUtil.isBlank( lostItems.getDefaultPicture()) && CollUtil.isNotEmpty(lostItems.getPicUrls())){
            lostItems.setDefaultPicture(lostItems.getPicUrls().get(0));
        }
        lostItems.getPicUrls().forEach(picUrl -> {
            LostFoundPic lostFoundPic = new LostFoundPic();
            lostFoundPic.setUrl(picUrl);
            System.out.println("有没有问题"+lostItems.getLostItemId());
            lostFoundPic.setLostItemId(lostItems1.getLostItemId());
            lostFoundPicService.save(lostFoundPic);
        });
    }

    @Override
    public LostFoundInfoVO getLostInfo(Integer id) {
        LostItems byId = getById(id);
        LostFoundInfoVO vo = new LostFoundInfoVO();
        BeanUtils.copyProperties(byId, vo);
        LostFoundCategory category = lostFoundCategoryService.getById(byId.getCategoryId());
        vo.setCategoryName(category.getName());
        vo.setPicUrls(lostFoundPicService.lambdaQuery().eq(LostFoundPic::getLostItemId, id).list().stream().map(LostFoundPic::getUrl).collect(Collectors.toList()));
        return vo;
    }
}




